<html>
	<head>
		<title>DummyChat</title>
		<style>
			body, table {
				font-family: verdana;
				font-size: 9pt
			}
		</style>
	</head>
	
	<body>
		<center>
			<h1>DummyChat</h1>
			<img src="icon.png">
			</br>
			<h2>Register a new account</h2>
			<table width=640px><tr><td>
				<?php
				if ($_POST['submit'] == 'Register now!')
				{
					if ( ($_POST['uname'] == '') || ($_POST['pwd'] == '') || ($_POST['rwd'] == '') ) {?>
						<p style="color:red">Fields cannot be empty!</p><?php
					}
					else if ( $_POST['pwd'] != $_POST['rwd']) {?>
						<p style="color:red">Password supplied do not coincide!</p><?php
					}
					
					try {
						$db = new SQLiteDatabase("dummychat.db", 0666, $sqlite_err);
						$fetch = $db -> query ('SELECT username FROM UserDB WHERE username = "'.$_POST['uname'].'"') -> fetch();
						if ( $fetch != FALSE ) {
							echo $a;?>
							<p style="color:red">Username not available. Please choose another username.</p><?php
						}
						else {
							$db -> query ('INSERT INTO UserDB (username,password) VALUES ("'.$_POST['uname'].'","'.$_POST['pwd'].'")');
							$fetch = $db -> query('SELECT UID FROM UserDB WHERE username="'.$_POST['uname'].'"') -> fetch();
							$db -> query ('CREATE TABLE List'.$fetch['UID'].'(ID INTEGER PRIMARY KEY, UID INT)');?>
							<p style="color:red">Congratulation! Your account has been successfully created!</p><?php
						}
					}
					catch (Exception $e) {?>
						<p style="color:red">Database error: <?=$sqlite_err?>. Please try again later.</p><?php
					}
					
					// Check for registration attempt or errors like
					// - Username exists
				}
				else {?>
					<p>Please fill in the form with your credentials.</p><?php
				}?>
				<form action="" method="POST">
					<center><table>
						<tr><td>Username:</td>       <td><input type="text" name="uname"></td></tr>
						<tr><td>Password:</td>       <td><input type="password" name="pwd"></td></tr>
						<tr><td>Repeat Password:</td><td><input type="password" name="rwd"></td></tr>
					<table></center>
					<input type="submit" value="Register now!" name="submit">
				</form>
			</td></tr></table>

		</center>
	</body>
</html>